package cn.jn.lhm.teacher.handler;

import cn.jn.lhm.teacher.entity.EduChapter;
import cn.jn.lhm.teacher.entity.chapter.ChapterVo;
import cn.jn.lhm.teacher.service.EduChapterService;
import cn.jn.lhm.utils.R;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LHM
 * @Tips 热爱每一天，奋斗每一天
 * @date 2020/6/2 - 21:29
 */
@RestController
@RequestMapping("/chapter")
public class EduChapterHandler {

    @Resource
    private EduChapterService eduChapterService;

    /**
     *
     * @param courseId 课程Id
     * @return 树形结构组装的课程章节小节信息
     */
    @GetMapping("/get/all/{id}")
    public R getChapterAndVideo(@PathVariable("id")String courseId){
        List<ChapterVo> list = eduChapterService.getAll(courseId);
        return R.ok().data("data", list);
    }

    @PostMapping("/add")
    public R addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return R.ok();
    }

    @GetMapping("/get/{id}")
    public R getChapterInfo(@PathVariable("id")String id){
        EduChapter eduChapter = eduChapterService.getChapterById(id);
        return R.ok().data("data", eduChapter);
    }

    @PostMapping("/update")
    public R updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable("id")String id){
        boolean flag =  eduChapterService.deleteById(id);
        if (flag){
            return R.ok();
        }else {
            return R.error();
        }

    }
}
